/*
 *  Copyright 2012 Yuriy Dunko.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.domain.impl;

import java.util.Date;
import java.util.List;
import org.opu.pc.domain.PublisherManagement;
import org.opu.pc.domain.dao.PublisherDao;
import org.opu.pc.domain.domains.Publisher;

/**
 *
 * @author Yuriy Dunko
 */
public class PublisherManagementImpl implements PublisherManagement {

    private PublisherDao publisherDao;

    public PublisherManagementImpl(PublisherDao publisherDao) {
        this.publisherDao = publisherDao;
    }

    @Override
    public List<Publisher> list() {
        return publisherDao.getList();
    }

    @Override
    public List<Publisher> searchByName(String name, String secondName,
            String lastName) {
        return publisherDao.searchPublishersByName(name, secondName, lastName);

    }
    
    @Override
    public List<Publisher> searchPublishersByStudentBelonging(Boolean isSruden) {
    	return publisherDao.searchPublishersByStudentBelonging(isSruden);
    }

    @Override
    public Publisher getByName(String name, String secondName, String lastName) {
        List<Publisher> list = publisherDao.getPublishersByName(name,
                secondName, lastName);

        if (list.isEmpty()){
            return null;
        }

        if (list.size() > 1){
            // TODO: Handle some how ...
        }

        return list.get(0);
    }

    @Override
    public List<Publisher> publishersForPublication(Integer publicationId) {
        return publisherDao.getPublishersForPublication(publicationId);
    }

    @Override
    public Publisher createNew(String name, String secondName, 
    		String lastName, boolean isStudent) {
        
    	Publisher publisher = new Publisher(-1, name, secondName, lastName);
        publisher.setIsStudent(isStudent);
        
        Integer save = publisherDao.save(publisher);
        return publisherDao.get(save);
    }

    @Override
    public void deletePublisher(Integer id){
        publisherDao.delete(publisherDao.get(id));
    }

    @Override
    public void updatePublisher(Publisher publisherBean){
        Publisher get = publisherDao.get(publisherBean.getId());
        get.setName(publisherBean.getName());
        get.setLastName(publisherBean.getLastName());
        get.setSecondName(publisherBean.getSecondName());
        get.setIsStudent(publisherBean.getIsStudent());
        publisherDao.saveOrUpdate(get);
    }

    @Override
    public void merge(Integer etalonId, List<Integer> duplicatorsId) {
        publisherDao.merge(etalonId, duplicatorsId);
    }

    @Override
    public List<Publisher> getPublishersWithPublications(Date start,
            Date end, Boolean isStudent, Boolean isVac) {
        return publisherDao.getPublishersWithPublications(start, end,
                isStudent, isVac);
    }

}
